package cn.edu.gduf.luowowo.mgrsite.web.listener;

import cn.edu.gduf.luowowo.article.domain.StrategyDetail;
import cn.edu.gduf.luowowo.article.service.IStrategyDetailService;
import cn.edu.gduf.luowowo.cache.domain.StrategyStatisVO;
import cn.edu.gduf.luowowo.cache.service.IStrategyStatisVoRedisService;
import cn.edu.gduf.luowowo.common.util.BeanUtil;
import cn.edu.gduf.luowowo.common.util.RedisKeys;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.List;

/**
 * 初始化redis中的数据 监听器
 * ApplicationListener:springboot监听器要实现这个接口
 * ContextRefreshedEvent:监听容器启动或重新加载
 */
//@Component //交给spring容器管理
public class RedisInitDataListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVoRedisService strategyStatisVoRedisService;

    /**
     * 在容器启动或容器重新加载的时候执行的方法
     * 做redis的数据初始化
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //测试
        System.out.print("       _,    _   _    ,_\n" +
                "  .o888P     Y8o8Y     Y888o.\n" +
                " d88888      88888      88888b\n" +
                "d888888b_  _d88888b_  _d888888b\n" +
                "8888888888888888888888888888888\n");
        //查询mysql数据库中的攻略明细，创建vo对象，存储vo到redis中
        List<StrategyDetail> details = strategyDetailService.list();
        String hotKey = RedisKeys.STARTEGY_ZSET_SORT_HOT.getPrefix();
        String commendKey = RedisKeys.STARTEGY_ZSET_SORT_COMMEND.getPrefix();
        //遍历，创建出一个个的vo对象
        for (StrategyDetail detail : details) {
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtil.copyProperties(vo, detail);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            //=======================初始化vo======================
            if (!strategyStatisVoRedisService.isVOExists(detail.getId())){
                strategyStatisVoRedisService.setVO(vo);
            }
            //=======================初始化hot======================
            if (!strategyStatisVoRedisService.isVOExistsInZSet(detail.getId(),hotKey)){
                strategyStatisVoRedisService.setStrategyScore(hotKey,vo.getViewnum()+vo.getReplynum(),
                        RedisKeys.STARTEGY_STATIS_VO.join(detail.getId().toString()));
            }
            //=======================初始化commend======================
            if (!strategyStatisVoRedisService.isVOExistsInZSet(detail.getId(),commendKey)){
                strategyStatisVoRedisService.setStrategyScore(commendKey,vo.getThumbsupnum()+vo.getFavornum(),
                        RedisKeys.STARTEGY_STATIS_VO.join(detail.getId().toString()));
            }
        }
        System.out.println("8888888888888888888888888888888\n" +
                "YJGS8P\"Y888P\"Y888P\"Y888P\"Y8888P\n" +
                " Y888   '8'   Y8P   '8'   888Y\n" +
                "  '8o          V          o8'\n" +
                "    `                     `");
    }
}
